package com.example.pokemon_chat_room.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.alibaba.fastjson.JSONObject;
import com.example.pokemon_chat_room.MainActivity;
import com.example.pokemon_chat_room.R;
import com.example.pokemon_chat_room.databinding.ActivityPokedexBinding;
import com.example.pokemon_chat_room.databinding.FragmentHomeBinding;
import com.example.pokemon_chat_room.pokedex;
import com.example.pokemon_chat_room.request.OkHttp;
import com.example.pokemon_chat_room.util.MyImageView;

import java.io.IOException;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    private ImageView pokeDex;

    private MyImageView catch_top_img;

    private MyImageView free_top_img;

    private MyImageView trade_top_img;



    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        pokeDex = (ImageView) root.findViewById(R.id.tujian);
        catch_top_img= root.findViewById(R.id.catch_top_img);
        free_top_img= root.findViewById(R.id.free_top_img);
        trade_top_img= root.findViewById(R.id.trade_top_img);
        pokeDex.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(root.getContext(), pokedex.class);
                startActivity(intent);
            }
        });


        OkHttp okHttp = new OkHttp();

        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject res = null;

                try {
                    res = okHttp.Post(getContext(), "testImg/getTopImg", null);
                    System.out.println(res);
                    JSONObject finalRes = res.getJSONObject("data");
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            catch_top_img.setImageURL("poke/"+finalRes.getString("catchTop"));
                            free_top_img.setImageURL("poke/"+finalRes.getString("freeTop"));
                            trade_top_img.setImageURL("poke/"+finalRes.getString("tradeTop"));
                        }
                    });
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }

            }
        }).start();

        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}